﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Intorise.API;
using MuBot;
using System.Threading;
using Model;
using Intorise.Common;
using System.Diagnostics;
using System.IO;

namespace MUClient
{
    public partial class ADForm : MainWindow
    {
        DateTime begin;

        public ADForm():base()
        {
            //InitializeComponent();
            this.Text = "自动世界聊天喊话";
        }

        protected override void FillAccounts(string filename, List<Player> list, bool clear)
        {
            if (clear)
            {
                list.Clear();
            }

            StreamReader sr = null;
            try
            {
                sr = new StreamReader(filename, Encoding.Default);
            }
            catch
            {
                throw;
            }
            try
            {
                while (!sr.EndOfStream)
                {
                    string[] items = sr.ReadLine().Split(new char[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if ((items.Length > 4))
                    {
                        if (items[0] == "#") { continue; }
                        Model.Account account = new Model.Account();
                        account.Username = items[0];
                        account.Password = items[1];
                        account.ServerID = items[2];
                        var player = new AdPlayer(account, config);

                        if (items.Length > 5)
                        {
                            player.CollectStoneMapLocation = Player.BotFightLocations.FirstOrDefault(f => f.MapName == items[3] && f.LocationName == items[4]);

                            int line = 0;
                            if (int.TryParse(items[5], out line))
                            {
                                player.CurrentLine = line;
                                player.SetLine = line;
                            }
                        }
                        if (items.Length > 6)
                        {
                            long masterid = 0;
                            if (long.TryParse(items[6], out masterid))
                            {
                                player.Master = new Warehouse { ID = masterid, Location = new Point(410, 132), Line = 1, CurrentMap = Maps.GetMap("地下城二层") };//仓库号的编号和停放地图地址
                            }
                        }
                        player.CompletedLevel = needLevel;
                        list.Add(player);
                    }
                }
                sr.Close();
            }
            catch
            {
                if (sr != null)
                {
                    sr.Close();
                }
                throw;
            }
        }

        protected override void MainThread()
        {
            Player p = null;
            Stopwatch watch = new Stopwatch();

            while (true)
            {
                watch.Start();
                for (var i = 0; i < players.Count; i++)
                {
                    p = players[i];
                    if (p.Status == PlayerStatusEnum.停止) { continue; }
                    if (p.LoginCount < p.Config.MaxLoginCount)
                    {
                        if (p.Login())
                        {
                            p.Play();
                        }
                        else
                        {
                            i--;
                        }
                    }
                    else
                    {
                        ConsoleBLL.WriteLine(p + " 登录尝试次数超过" + p.Config.MaxLoginCount + "次");
                        p.Status = PlayerStatusEnum.停止;
                    }
                }
                watch.Stop();
                if (watch.Elapsed.TotalMinutes < 15)
                {
                    Thread.Sleep(60 * 1000 * (15 - (int)watch.Elapsed.TotalMinutes));
                }
            }
        }
    }
}
